//
//  XGLoginRegisterViewController.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/13.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "XGLoginRegisterViewController.h"

#import "XGTextField.h"

#pragma mark - XGTopMenuView

@interface XGTopMenuView : UIView

/// 关闭按钮
@property (nonatomic,weak) UIButton *closeButton;
/// 登录注册按钮
@property (nonatomic,weak) UIButton *loginRegisterButton;

@end

@implementation XGTopMenuView

#pragma mark  构造方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI
{
    // 创建子控件
    UIButton *loginRegisterButton = [UIButton buttonWithTitle:@"注册账号" fontSize:15 titleColor:[UIColor whiteColor] target:nil action:nil];
    [loginRegisterButton setTitle:@"已有账号?" forState:UIControlStateSelected];
    UIButton *closeButton = [UIButton buttonWithImageName:@"login_close_icon" highlightedImageName:@"login_close_icon" target:nil action:nil];
    
    // 添加子控件
    [self addSubview:loginRegisterButton];
    [self addSubview:closeButton];
    
    // 子控件赋值
    _loginRegisterButton = loginRegisterButton;
    _closeButton = closeButton;
    
    // 设置自动布局
    [loginRegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(30);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-30);
    }];
}

@end

#pragma mark - XGLoginRegisterView

typedef NS_ENUM(NSUInteger, XGLoginRegisterViewType) {
    XGLogin = 0,
    XGRegister = 1,
};

@interface XGLoginRegisterView : UIView

+ (instancetype)loginRegisterViewWithType:(XGLoginRegisterViewType)type;
- (instancetype)initWithType:(XGLoginRegisterViewType)type;

@end

@implementation XGLoginRegisterView

#pragma mark  构造方法

- (instancetype)initWithType:(XGLoginRegisterViewType)type
{
    if (self = [super initWithFrame:CGRectZero]) {
        [self setUpUIWithType:type];
    }
    
    return self;
}

+ (instancetype)loginRegisterViewWithType:(XGLoginRegisterViewType)type
{
    return [[self alloc] initWithType:type];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUIWithType:XGLogin];
    }
    
    return self;
}

- (void)setUpUIWithType:(XGLoginRegisterViewType)type
{
    // 创建子控件
    UIImageView *editBackgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_rgister_textfield_bg"]];
    
    XGTextField *accountTextField = [[XGTextField alloc] init];
    accountTextField.placeholder = (type == XGLogin ? @"手机号" : @"请输入手机号");
    accountTextField.font = [UIFont systemFontOfSize:20];
    accountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    accountTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    XGTextField *passwordTextField = [[XGTextField alloc] init];
    passwordTextField.placeholder = (type == XGLogin ? @"密码" : @"请设置密码");
    passwordTextField.secureTextEntry = YES;
    passwordTextField.font = [UIFont systemFontOfSize:20];
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIButton *loginRegisterButton = [UIButton buttonWithTitle:(type == XGLogin ? @"登录" : @"注册") fontSize:17 titleColor:[UIColor whiteColor] backgroundImageName:@"login_register_button" highlightedBackgroundImageName:@"login_register_button_click" target:nil action:nil];
    loginRegisterButton.layer.cornerRadius = 5;
    loginRegisterButton.layer.masksToBounds = YES;
    
    // 添加子控件
    [self addSubview:editBackgroundImageView];
    [self addSubview:accountTextField];
    [self addSubview:passwordTextField];
    [self addSubview:loginRegisterButton];
    
    // 设置自动布局
    [editBackgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(10);
        make.size.mas_equalTo(CGSizeMake(266, 92));
    }];
    
    [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(editBackgroundImageView).offset(10);
        make.right.equalTo(editBackgroundImageView).offset(-10);
        make.top.equalTo(editBackgroundImageView);
        make.height.mas_equalTo(46);
    }];
    
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(accountTextField);
        make.bottom.equalTo(editBackgroundImageView);
    }];
    
    [loginRegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(editBackgroundImageView.mas_bottom).offset(10);
        make.centerX.equalTo(editBackgroundImageView);
        make.width.equalTo(editBackgroundImageView.mas_width);
        make.height.mas_equalTo(44);
    }];
    
    if (type == XGLogin) {
        UIButton *forgetPasswordButton = [UIButton buttonWithTitle:@"忘记密码?" fontSize:12 titleColor:[UIColor whiteColor] target:nil action:nil];
        [self addSubview:forgetPasswordButton];
        [forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(loginRegisterButton.mas_bottom).offset(10);
            make.right.equalTo(loginRegisterButton);
        }];
    }
}

@end

#pragma mark - XGOtherLoginView

@interface XGOtherLoginView : UIView

@end

@implementation XGOtherLoginView

#pragma mark  构造方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI
{
    // 创建子控件
    UIButton *qqLoginButton = [UIButton buttonWithTitle:@"QQ登录" fontSize:15 titleColor:[UIColor whiteColor] imageName:@"login_QQ_icon" highlightedImageName:@"login_QQ_icon_click" target:nil action:nil];
    [qqLoginButton layoutButtonWithEdgeInsetsStyle:KButtonEdgeInsetsStyleTop imageTitleSpace:0];
    UIButton *sinaLoginButton = [UIButton buttonWithTitle:@"新浪微博登录" fontSize:15 titleColor:[UIColor whiteColor] imageName:@"login_sina_icon" highlightedImageName:@"login_sina_icon_click" target:nil action:nil];
    [sinaLoginButton layoutButtonWithEdgeInsetsStyle:KButtonEdgeInsetsStyleTop imageTitleSpace:0];
    UIButton *tecentWeiboLoginButton = [UIButton buttonWithTitle:@"腾讯微博登录" fontSize:15 titleColor:[UIColor whiteColor] imageName:@"login_tecent_icon" highlightedImageName:@"login_tecent_icon_click" target:nil action:nil];
     [tecentWeiboLoginButton layoutButtonWithEdgeInsetsStyle:KButtonEdgeInsetsStyleTop imageTitleSpace:0];
    
    UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[qqLoginButton,sinaLoginButton,tecentWeiboLoginButton]];
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.axis = UILayoutConstraintAxisHorizontal;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 10;
    
    // 添加子控件
    [self addSubview:stackView];
    
    // 设置自动布局
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.height.mas_equalTo(self);
    }];
}

@end

#pragma mark - XGLoginRegisterViewController

@interface XGLoginRegisterViewController ()

/// 顶部菜单视图
@property (nonatomic,strong) XGTopMenuView *topMenuView;
/// 登录注册视图
@property (nonatomic,strong) UIView *loginRegisterView;
/// 其他登录视图
@property (nonatomic,strong) XGOtherLoginView *otherLoginView;

@end

@implementation XGLoginRegisterViewController

#pragma mark  控制器生命周期方法

- (void)loadView
{
    [super loadView];
    
    // 设置视图层次
    [self setUpUI];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 顶部菜单添加事件监听
    [self addTopMenuViewAction];
}

#pragma mark 事件监听

- (void)closeLoginRegisterAction
{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loginRegisterAction:(UIButton *)button
{
    button.selected = !button.isSelected;
    CGFloat offset = button.isSelected ? -self.loginRegisterView.width / 2 : 0;
    [UIView animateWithDuration:1.0 animations:^{
        self.loginRegisterView.transform = CGAffineTransformMakeTranslation(offset, 0);
    }];
}

#pragma mark  系统方法

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark 内部私有其他方法

// 设置界面
- (void)setUpUI
{
    // 创建子控件
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_register_background"]];
    
    // 添加子控件
    [self.view addSubview:backgroundImageView];
    [self.view addSubview:self.topMenuView];
    [self.view addSubview:self.loginRegisterView];
    [self.view addSubview:self.otherLoginView];
    
    // 设置自动布局
    [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.topMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    [self.loginRegisterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topMenuView.mas_bottom).offset(10);
        make.left.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(2);
        make.height.mas_equalTo(200);
    }];
    
    [self.otherLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(90);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-80);
    }];
}

// 顶部菜单添加事件监听
- (void)addTopMenuViewAction
{
    [self.topMenuView.closeButton addTarget:self action:@selector(closeLoginRegisterAction) forControlEvents:UIControlEventTouchUpInside];
    [self.topMenuView.loginRegisterButton addTarget:self action:@selector(loginRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 懒加载

- (XGTopMenuView *)topMenuView
{
    if (_topMenuView == nil) {
        _topMenuView = [[XGTopMenuView alloc] init];
        _topMenuView.backgroundColor = [UIColor clearColor];
    }
    
    return _topMenuView;
}

- (UIView *)loginRegisterView
{
    if (_loginRegisterView == nil) {
        _loginRegisterView = [self setUpLoginRegisterView];
        _loginRegisterView.backgroundColor = [UIColor clearColor];
    }
    
    return _loginRegisterView;
}

- (XGOtherLoginView *)otherLoginView
{
    if (_otherLoginView == nil) {
        _otherLoginView = [[XGOtherLoginView alloc] init];
        _otherLoginView.backgroundColor = [UIColor clearColor];
    }
    
    return _otherLoginView;
}

- (UIView *)setUpLoginRegisterView
{
    UIView *loginRegisterView = [[UIView alloc] init];
    // 创建子控件
    XGLoginRegisterView *loginView = [XGLoginRegisterView loginRegisterViewWithType:XGLogin];
    loginView.backgroundColor = [UIColor clearColor];
    XGLoginRegisterView *registerView = [XGLoginRegisterView loginRegisterViewWithType:XGRegister];
    registerView.backgroundColor = [UIColor clearColor];
    
    // 添加子控件
    [loginRegisterView addSubview:loginView];
    [loginRegisterView addSubview:registerView];
    
    // 设置自动布局
    [loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(loginRegisterView);
        make.width.equalTo(loginRegisterView).multipliedBy(0.5);
    }];
    
    [registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(loginView.mas_right);
        make.top.bottom.equalTo(loginRegisterView);
        make.width.equalTo(loginRegisterView).multipliedBy(0.5);
    }];
    
    return loginRegisterView;
}

@end
